package com.shopping_assistant.view.overlays;

import java.text.DecimalFormat;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MyLocationOverlay;
import com.shopping_assistant.model.Shop;
import com.shopping_assistant.view.R;

public class SAPopUp {

	private String phoneNumer = "";
	private Context mContext;
	private MyLocationOverlay myLocationOverlay;
	private SAItemizedOverlay sAItemizedOverlay;
	private Dialog dialog;

	public SAPopUp(SAItemizedOverlay sAItemizedOverlay, MyLocationOverlay myLocationOverlay, Context context) {
		super();
		
		this.myLocationOverlay = myLocationOverlay;
		this.mContext = context;
		this.sAItemizedOverlay = sAItemizedOverlay;
		dialog = new Dialog(mContext);
	}

	public boolean showShopDialog(final Shop shop) {

		dialog.setContentView(R.layout.sadialog);

		dialog.setTitle(shop.getName());

		TextView info = (TextView) dialog.findViewById(R.id.sadialog_info);
		info.setText(shop.getDescription());

		GeoPoint geo = myLocationOverlay.getMyLocation();
		
		String sdistance = mContext.getString(R.string.txt_sadialog_nodistance_msg);
		TextView distance = (TextView) dialog.findViewById(R.id.sadialog_distance);
		
		if (geo != null) {
			Location locurrent = new Location("");
			locurrent.setLatitude(geo.getLatitudeE6() / (double) 1E6);
			locurrent.setLongitude(geo.getLongitudeE6() / (double) 1E6);

			Location shopLocation = new Location("");
			shopLocation.setLatitude(shop.getLatitude());
			shopLocation.setLongitude(shop.getLongitude());
			
			sdistance = convertDistance(locurrent.distanceTo(shopLocation));
		}
		
		distance.setText(sdistance);
		
		TextView open = (TextView) dialog.findViewById(R.id.sadialog_open);
		
		open.setText(String.format("%s - %s", shop.getOpen(), shop.getClose()));

		TextView phone = (TextView) dialog.findViewById(R.id.sadialog_phone);
		phone.setText(shop.getPhone());
		
		Button buttonPhone = (Button) dialog.findViewById(R.id.sadialog_phone_button);
		buttonPhone.setText("Go to homepage");
		buttonPhone.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				Uri uri = Uri.parse(shop.getHomepage());
				mContext.startActivity(new Intent(Intent.ACTION_VIEW, uri)); 
			}
		});
		
		dialog.setOnDismissListener(new OnDismissListener() {
			
			public void onDismiss(DialogInterface dialog) {
				sAItemizedOverlay.dialogClosing();
			}
		});

		dialog.show();

		return true;
	}
	
	public void closeDialog() {
		dialog.dismiss();
	}

	private void call(CharSequence phoneNumber) {
		if (Pattern.matches(mContext.getString(R.string.phonePattern),
				phoneNumber)) {
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + phoneNumber));
			mContext.startActivity(callIntent);
		}

		else {
			Toast toast = Toast.makeText(mContext,
					"Your input is not a real phone number", 5);
			toast.show();
		}
	}

	private String convertDistance(double distance) {

		DecimalFormat f = new DecimalFormat("0.00");

		if (distance >= 1000.0)
			return f.format(distance / 1000) + " km";
		else if (distance > 0)
			return f.format(distance) + " m";
		else
			return f.format(distance * 1000) + " cm";
	}
}